//
//  ViewController.m
//  AVFoundation_erweimademo
//
//  Created by xiao han on 2016/12/15.
//  Copyright © 2016年 xiao han. All rights reserved.
//

#import "ViewController.h"
#import "ScanCode.h"
@interface ViewController ()<Erweimadelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    ScanCode *scncodeview = [[ScanCode alloc]initWithFrame:self.view.bounds];

    scncodeview.delegate = self;

    [self.view addSubview:scncodeview];

    UIButton *PhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    PhotoBtn.frame = CGRectMake(150, 50, 50, 50);

    [PhotoBtn setTitle:@"相册" forState:UIControlStateNormal];

    [PhotoBtn addTarget:self action:@selector(choicePhoto) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:PhotoBtn];

}

- (void)choicePhoto{
    //调用相册
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}
//选中图片的回调
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *content = @"" ;
    //取出选中的图片
    UIImage *pickImage = info[UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImagePNGRepresentation(pickImage);
    CIImage *ciImage = [CIImage imageWithData:imageData];
    
    //创建探测器
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
    NSArray *feature = [detector featuresInImage:ciImage];
    
    //取出探测到的数据
    for (CIQRCodeFeature *result in feature)
    {
        //进行处理(音效、网址分析、页面跳转等)
        content = result.messageString;
        
        NSString *regex =@"[a-zA-z]+://[^\\s]*";
        
        
        NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        
        if ([urlTest evaluateWithObject:content])
        {
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:content] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@""}  completionHandler:nil];
        }
    }
  
    
    //关闭相册界面
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)dataString:(NSString*)StringValue
{

    //输出扫描字符串
    NSLog(@"hanxiao:%@",StringValue);
    
    NSString *regex =@"[a-zA-z]+://[^\\s]*";
    
    
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    if ([urlTest evaluateWithObject:StringValue])
    {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:StringValue] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@""}  completionHandler:nil];
    }
    
    



}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
